package com.xian.im.modules.message;

import com.xian.feign.UserChannelClient;
import com.xian.im.common.constant.CommandCodeConstant;
import com.xian.im.common.constant.CommandConstant;
import com.xian.im.common.enums.ConsumerQueueEnum;
import com.xian.im.common.proto.MsgRequest;
import com.xian.im.common.proto.MsgResponse;
import com.xian.im.common.utils.sys.SpringContextHolder;
import com.xian.im.common.utils.template.RabbitTemplateUtil;
import com.xian.im.message.model.QueueMsg;
import lombok.extern.slf4j.Slf4j;

/**
 * @program: xian-im-server
 * @description:
 * @author: liru.xian
 * @create: 2021-01-26 10:11
 */
@Slf4j
public class UserHandler {

  /**
   * 系统消息
   * @param body
   * @param userId
   */
  public static void sendSystemMessage(String body, String userId) {
    MsgRequest request = new MsgRequest();
    request.setToAccount(userId);
    request.setCmd(CommandConstant.SYSTEM_CMD);
    request.setCmdCode(CommandCodeConstant.SYSTEM_USER);
    request.setBody(body);
    QueueMsg msg = new QueueMsg();
    msg.setAnEnum(ConsumerQueueEnum.CONSUMER_SYSTEM);
    msg.setMsgRequest(request);
    RabbitTemplateUtil.sendMes(ConsumerQueueEnum.CONSUMER_SYSTEM.getExchange(),ConsumerQueueEnum.CONSUMER_SYSTEM.getFactoryName(),msg);
  }

  /**
   * 点对点消息
   *
   * @param channelId
   */
  public static void sendSystemMessage(MsgResponse response, String channelId) {
    UserChannelClient client = SpringContextHolder.getBean(UserChannelClient.class);
    client.pushChannelSystemMessage(channelId,response);
  }

  /**
   * 点对点消息
   *
   * @param channelId
   */
  public static void sendMessage(MsgResponse response, String channelId) {
    UserChannelClient client = SpringContextHolder.getBean(UserChannelClient.class);
    client.pushChannelMessage(channelId,response);
  }
}
